package com.bjgzy.rating.mvp.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bjgzy.rating.R;
import com.bjgzy.rating.R2;
import com.bjgzy.rating.di.component.DaggerWinningWorkComponent;
import com.bjgzy.rating.di.module.WinningWorkModule;
import com.bjgzy.rating.mvp.contract.WinningWorkContract;
import com.bjgzy.rating.mvp.presenter.WinningWorkPresenter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.eduhzy.ttw.commonres.utils.RxUtil;
import com.eduhzy.ttw.commonsdk.core.Constants;
import com.eduhzy.ttw.commonsdk.core.RouterHub;
import com.eduhzy.ttw.commonsdk.decoration.GridSpacingItemDecoration;
import com.eduhzy.ttw.commonsdk.entity.EntriesData;
import com.eduhzy.ttw.commonsdk.entity.RatingHomeData;
import com.eduhzy.ttw.commonsdk.holder.AutoBaseViewHolder;
import com.jess.arms.base.BaseFragment;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.luck.picture.lib.tools.DoubleUtils;
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;

import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;

import static com.jess.arms.utils.Preconditions.checkNotNull;

/**
 * 获奖作品
 *
 * @author xubo
 * @date 2018-8-21 18:57:29
 */
@Route(path = RouterHub.RATING_WINNINGWORKFRAGMENT)
public class WinningWorkFragment extends BaseFragment<WinningWorkPresenter> implements WinningWorkContract.View {
    @BindView(R2.id.rv_list)
    RecyclerView rvList;
    @BindView(R2.id.swipeLayout)
    SwipeRefreshLayout mSwipeLayout;
    @Inject
    BaseQuickAdapter<EntriesData, AutoBaseViewHolder> mAdapter;
    @Inject
    List<EntriesData> lists;
    @Inject
    RecyclerView.LayoutManager mManager;
    private RatingHomeData mData;


    public static WinningWorkFragment newInstance(RatingHomeData data) {
        WinningWorkFragment fragment = new WinningWorkFragment();
        Bundle args = new Bundle();
        args.putParcelable(Constants.ROUTER_DATA, data);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void setupFragmentComponent(@NonNull AppComponent appComponent) {
        DaggerWinningWorkComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .winningWorkModule(new WinningWorkModule(this))
                .build()
                .inject(this);
        mData = getArguments().getParcelable(Constants.ROUTER_DATA);
    }

    @Override
    public View initView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.rating_fragment_winning_work, container, false);
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        mSwipeLayout.setOnRefreshListener(() -> {
            getDatas(true);
        });
        GridSpacingItemDecoration decoration = new GridSpacingItemDecoration(2, RxUtil.getAutoWidth(getActivity(),20), true);
        rvList.setLayoutManager(mManager);
        rvList.setAdapter(mAdapter);
        rvList.addItemDecoration(decoration);
        mAdapter.setOnItemClickListener((adapter, view, position) -> {
            if (!DoubleUtils.isFastDoubleClick()) {
                TextView no = view.findViewById(R.id.tv_no);
                TextView name = view.findViewById(R.id.tv_name);
                ARouter.getInstance()
                        .build(RouterHub.RATING_ENTRIESDETAILACTIVITY)

                        .withParcelable(Constants.ROUTER_DATA, mAdapter.getData().get(position))
                        .withString(Intent.EXTRA_TITLE, no.getText().toString() + name.getText().toString())
                        .withBoolean(Constants.ROUTER_BOOLEAN2, true)
                        .navigation();
            }
        });
        mAdapter.setOnLoadMoreListener(() -> getDatas(false), rvList);
        getDatas(true);
    }

    private void getDatas(boolean pullToRefresh) {
        mPresenter.getAwardWorksByResearchId(pullToRefresh, mData.getId());
    }

    @Override
    public void setData(@Nullable Object data) {

    }

    @Override
    public void showLoading() {
        mSwipeLayout.setRefreshing(true);
        mAdapter.setEnableLoadMore(false);
    }

    @Override
    public void hideLoading() {
        mSwipeLayout.setRefreshing(false);
        mAdapter.setEnableLoadMore(true);
    }


    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        RxUtil.showTipDialog(getActivity(), QMUITipDialog.Builder.ICON_TYPE_NOTHING, message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {

    }

    @Override
    public void addFooter() {
        View view = new View(getActivity());
        ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, RxUtil.getAutoHeight(getActivity(), 270));
        view.setLayoutParams(layoutParams);
        mAdapter.addFooterView(view);
    }
}
